//
//  TT_IMContactManager.m
//  BaseProject
//
//  Created by 王洋 on 2020/9/4.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "TT_IMContactManager.h"
#import "TT_ConversationDetailViewController.h"

@interface TT_IMContactManager ()

@property (nonatomic, assign) BOOL isLoading;

@end


@implementation TT_IMContactManager

+ (TT_IMContactManager *)manager {
    
    static TT_IMContactManager *shareInstance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken,^{
        shareInstance = [TT_IMContactManager new];
    });
    return shareInstance;
}

- (void)ImContactWithStoreId:(NSString *)storeId{
    
    if (self.isLoading) {
        return;
    }
    [self request_StoreImWithStoreId:storeId];
}
- (void)ImContactWithStoreId:(NSString *)storeId withQuickReply:(NSString *)quickReply
{
    if (self.isLoading) {
        return;
    }
    [self request_StoreImWithStoreId:storeId quickReply:quickReply];
}

- (void)ImContactWithType:(TT_IMContactType)type {
    if (self.isLoading) {
        return;
    }
    [self request_ServiceWuthType:type];
}
- (void)ImContactWithType:(TT_IMContactType)type withQuickReply:(NSString *)quickReply
{
    if (self.isLoading) {
        return;
    }
    [self request_ServiceWuthType:type quickReply:quickReply];
}


- (void)request_StoreImWithStoreId:(NSString *)storeId {
    
    self.isLoading = YES;
    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setValue:storeId forKey:@"sellerId"];
    [dic setValue:[TTUserModel sharedObject].userId forKey:@"userId"];
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_GetStoreImId];

    WEAKSELF
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {

        if ([data isKindOfClass:[NSDictionary class]]) {
            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                
                NSDictionary *resultDic = [data valueForKey:@"data"];
                if (![resultDic isKindOfClass:[NSDictionary class]]) {
                    return;
                }
                NSString *groupId = [resultDic valueForKey:@"groupId"];

                TUIConversationCellData *data = [[TUIConversationCellData alloc] init];
                data.groupID = groupId;
                TT_ConversationDetailViewController *chat = [[TT_ConversationDetailViewController alloc] init];
                chat.conversationData = data;
                
                [SVProgressHUD showWithStatus:@"正在构建群聊"];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

                    [SVProgressHUD dismiss];
                    TT_NavigationViewController *navController = (TT_NavigationViewController *)[TT_TabbarViewController shareTabbar].selectedViewController;
                    [navController pushViewController:chat animated:YES];
                });
                                
            } else {
                
                [SVProgressHUD showErrorWithStatus:[data valueForKey:@"message"]];
                [SVProgressHUD dismissWithDelay:1];
            }
        }
        
        weakSelf.isLoading = NO;
    } FailedBlock:^(NSError * _Nonnull error) {
        weakSelf.isLoading = NO;

    }];
    
}
- (void)request_StoreImWithStoreId:(NSString *)storeId quickReply:(NSString *)quickReply {
    
    self.isLoading = YES;
    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setValue:storeId forKey:@"sellerId"];
    [dic setValue:[TTUserModel sharedObject].userId forKey:@"userId"];
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_GetStoreImId];

    WEAKSELF
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {

        if ([data isKindOfClass:[NSDictionary class]]) {
            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                
                NSDictionary *resultDic = [data valueForKey:@"data"];
                if (![resultDic isKindOfClass:[NSDictionary class]]) {
                    return;
                }
                NSString *groupId = [resultDic valueForKey:@"groupId"];

                TUIConversationCellData *data = [[TUIConversationCellData alloc] init];
                data.groupID = groupId;
                TT_ConversationDetailViewController *chat = [[TT_ConversationDetailViewController alloc] init];
                chat.quickReply = quickReply;
                chat.conversationData = data;
                
                [SVProgressHUD showWithStatus:@"正在构建群聊"];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

                    [SVProgressHUD dismiss];
                    TT_NavigationViewController *navController = (TT_NavigationViewController *)[TT_TabbarViewController shareTabbar].selectedViewController;
                    [navController pushViewController:chat animated:YES];
                });
                                
            } else {
                
                [SVProgressHUD showErrorWithStatus:[data valueForKey:@"message"]];
                [SVProgressHUD dismissWithDelay:1];
            }
        }
        
        weakSelf.isLoading = NO;
    } FailedBlock:^(NSError * _Nonnull error) {
        weakSelf.isLoading = NO;

    }];
    
}


- (void)request_ServiceWuthType:(TT_IMContactType)type quickReply:(NSString *)quickReply {
    NSMutableDictionary *dic = @{}.mutableCopy;
    
    [dic setValue:@(type) forKey:@"serviceType"];
    [dic setValue:[TTUserModel sharedObject].userId forKey:@"userId"];
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_GetImId];
    
    self.isLoading = YES;
    WEAKSELF
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {

        if ([data isKindOfClass:[NSDictionary class]]) {
            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {

                NSDictionary *resultDic = [data valueForKey:@"data"];
                if (![resultDic isKindOfClass:[NSDictionary class]]) {
                    return;
                }
                NSString *userName = [resultDic valueForKey:@"userName"];
                NSString *nickName = [resultDic valueForKey:@"nickName"];

                TUIConversationCellData *data = [[TUIConversationCellData alloc] init];
                data.userID = userName;
                data.title = nickName;
                
                TT_ConversationDetailViewController *chat = [[TT_ConversationDetailViewController alloc] init];
                chat.quickReply = quickReply;
                chat.conversationData = data;
                TT_NavigationViewController *navController = (TT_NavigationViewController *)[TT_TabbarViewController shareTabbar].selectedViewController;
                [navController pushViewController:chat animated:YES];
                
            } else {
                [SVProgressHUD showErrorWithStatus:[data valueForKey:@"message"]];
                [SVProgressHUD dismissWithDelay:1];
            }
        }
        weakSelf.isLoading = NO;
    } FailedBlock:^(NSError * _Nonnull error) {
        weakSelf.isLoading = NO;

    }];
}
- (void)request_ServiceWuthType:(TT_IMContactType)type {
    
    NSMutableDictionary *dic = @{}.mutableCopy;
    
    [dic setValue:@(type) forKey:@"serviceType"];
    [dic setValue:[TTUserModel sharedObject].userId forKey:@"userId"];
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_GetImId];
    
    self.isLoading = YES;
    WEAKSELF
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {

        if ([data isKindOfClass:[NSDictionary class]]) {
            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {

                NSDictionary *resultDic = [data valueForKey:@"data"];
                if (![resultDic isKindOfClass:[NSDictionary class]]) {
                    return;
                }
                NSString *userName = [resultDic valueForKey:@"userName"];
                NSString *nickName = [resultDic valueForKey:@"nickName"];

                TUIConversationCellData *data = [[TUIConversationCellData alloc] init];
                data.userID = userName;
                data.title = nickName;
                
                TT_ConversationDetailViewController *chat = [[TT_ConversationDetailViewController alloc] init];
                chat.conversationData = data;
                TT_NavigationViewController *navController = (TT_NavigationViewController *)[TT_TabbarViewController shareTabbar].selectedViewController;
                [navController pushViewController:chat animated:YES];
                
            } else {
                [SVProgressHUD showErrorWithStatus:[data valueForKey:@"message"]];
                [SVProgressHUD dismissWithDelay:1];
            }
        }
        weakSelf.isLoading = NO;
    } FailedBlock:^(NSError * _Nonnull error) {
        weakSelf.isLoading = NO;

    }];
    
}


@end
